import os
import pickle
from time import strftime


# （管理员账户：admin/123456,普通用户:lisi/123456）

# 添加账务
def save(fname):
    id = input('ID:')
    kind = input('类别:')
    account = input('账户:')
    amount = input('金额:')
    date = strftime('%Y-%m-%d')
    comment = input('备注:')

    # 从文件中取出全部记录
    with open(fname, 'rb') as fobj:
        recods = pickle.load(fobj)

    # 添加记录
    recod = [id, kind, account, amount, date, comment]
    recods.append(recod)

    # 将更新后的数据重新写入文件
    with open(fname, 'wb')as fobj:
        pickle.dump(recods, fobj)


# 编辑账务
def edit(fname):
    print("等待开发")


# 删除账务
def delete(fname):
    print("等待开发")


# 查询账务
def query(fname):
    with open(fname, 'rb') as fobj:
        recods = pickle.load(fobj)
    print('%-12s%-12s%-12s%-12s%-12s%-12s' % ('ID', '类别', '账户', '余额', '时间', '说明'))
    for j in tuple(recods):
        print('%-12s%-12s%-12s%-12s%-12s%-12s' % tuple(j))
    print("-------------------------------------------")


# 显示
def show_menu():
    prompt = '''1 添加账务   2 编辑账务   3 删除账务   4 查询账务   5 退出系统
请输入你想要的操作：'''
    cmds = {'1': save, '2': edit, '3': delete, '4': query}
    fname = 'account.data'
    # 起始数据来一个
    init_data = [
        [1, "彩票", "现金", 1000000, strftime('%Y-%m-%d'), "100w到手"]
    ]
    # 如果文件不存在,把初始化数据写进去
    if not os.path.exists(fname):
        with open(fname, 'wb') as fobj:
            pickle.dump(init_data, fobj)

    while True:
        choice = input(prompt).strip()
        if choice not in ['1', '2', '3', '4', '5']:
            print('无效输入')
            continue
        if choice == '5':
            print('\nBye~~')
            break
        cmds[choice](fname)


if __name__ == '__main__':
    i = 0
    for i in range(3):
        print("-----------请输入用户名和密码进行登录------------")
        name = input("账户：")
        pwd = input("密码：")
        if name == "admin" and pwd == "123456" or name == "lisi" and pwd == "123456":
            print("恭喜您！登录成功！")
            if name == "admin":
                show_menu()
            else:
                print("普通账户就看看静态页面好了")
            break
        else:
            i += 1
            print("输出错误，请重试！")
            continue

    if i == 3:
        print("请稍等一会再进行登录！！！")
